package com.itheima.reggie.mapper;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DishMapper {

    //根据类别id查询该类别菜品数量
    @Select("select count(*) from dish where category_id=#{categoryId} and is_deleted=0")
    Integer CountByCid(Long categoryId);

    //插入菜品数量
    @Insert("insert into dish values (null,#{name},#{categoryId},#{price},#{code},#{image},#{description}," +
            "#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //默认情况:数据库表中这条记录有主键,但是dishDto的属性id为null
    //目标:执行插入后将数据库上成的id数据返回设置给dishDto对象中id属性
    //useGeneratedKeys = true 获取生成的主键
    //keyColumn = "id",keyProperty = "id" 将数据库表中生成的id值赋值给dishDto对象的属性id
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    Integer insert(DishDto dishDto);

    //分页查询:多表连接查询
    List<DishDto> findAll(@Param("name") String name);

    //根据菜品id查询一个菜品(一个菜品多个口味)
    DishDto findById(Long id);

    //根据id修改菜品
    Integer update(DishDto dishDto);

    //根据类别id查询菜品列表的方法
    @Select("select * from dish where category_id=#{categoryId} and is_deleted=0 order by sort asc")
    List<Dish> findByCategoryId(Long categoryId);

    //根据类别id和状态查询菜品列表
    @Select("select * from dish " +
            "where category_id=#{categoryId} and is_deleted=0 and status=#{status} " +
            "order by sort asc")
    List<Dish> findByCategoryIdAndStatus(@Param("categoryId") Long categoryId,
                                         @Param("status") Integer status);
}
